package com.penngo.gui.dfdiff.view;

import com.penngo.gui.dfdiff.DFMain;
import com.penngo.gui.dfdiff.utils.GB;
import com.penngo.gui.dfdiff.utils.LogUtils;
import com.penngo.gui.dfdiff.utils.ScreenUtil;

import javax.swing.*;
import javax.swing.event.TreeExpansionEvent;
import java.awt.*;
import java.io.File;
import java.util.logging.Logger;

public class DesktopFrameFolderDiff extends DesktopFrame{


    public DesktopFrameFolderDiff(ProjectNode node) {
        super(node);
        this.getContentPane().add(new FolderTabPanel());
    }

    class FolderTabPanel extends JPanel {
        private FolderNode root = new FolderNode("根目录", 0, -1L);

        public FolderTabPanel(){
            setLayout(new BorderLayout());
            final JSplitPane hSplitPane = new JSplitPane();
            FolderComparePanel leftPanel = new FolderComparePanel(root, FolderTable.Type.LEFT);
            hSplitPane.setLeftComponent(leftPanel);

            FolderComparePanel rightPanel = new FolderComparePanel(root, FolderTable.Type.RIGHT);
            hSplitPane.setRightComponent(rightPanel);

            leftPanel.setOtherFolderCompare(rightPanel);
            rightPanel.setOtherFolderCompare(leftPanel);

            hSplitPane.setDividerLocation((int)(ScreenUtil.getScreenWidth() * 0.8 / 2));
            add(hSplitPane, BorderLayout.CENTER);

        }
    }

    class FolderComparePanel extends JPanel {
        private Logger log = LogUtils.getLogger(FolderComparePanel.class);
        private JTextField folderField;
        private JButton folderButton;
        private FolderTable treeTable;
        private FolderNode root;
        private FolderComparePanel otherFolderCompare;
        private FolderTable.Type type;
        private JFileChooser fileChooser;
        public FolderComparePanel(FolderNode root, FolderTable.Type type){
            this.root = root;
            this.type = type;
            this.setLayout(new GridBagLayout());
            initPanel(type);
        }

        private JPanel initAddressPanel(){
            JPanel panel = new JPanel();
            panel.setLayout(new GridBagLayout());
            folderField = new JTextField();
            if(type == FolderTable.Type.LEFT){
                if(node.getPathLeft() != null){
                    folderField.setText(node.getPathLeft().getAbsolutePath());
                }

            }
            else{
                if(node.getPathRight() != null){
                    folderField.setText(node.getPathRight().getAbsolutePath());
                }
            }
            folderButton = new JButton("选择");
            folderButton.addActionListener(l-> selectPath(null));
            panel.add(folderField, new GB(0, 0, 1,1).setFill(GB.BOTH).setWeight(100, 0).setInsets(5,5,5,5));
            panel.add(folderButton, new GB(1, 0, 1,1).setInsets(5,0,5,5));
            return panel;
        }
        private void selectPath(ProjectNode node){
            if(fileChooser == null){
                fileChooser = new JFileChooser(".");
                fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            }
            File selectFile = null;
            if(node == null){
                int result = fileChooser.showOpenDialog(null);
                if(result == JFileChooser.APPROVE_OPTION){
                    selectFile = fileChooser.getSelectedFile();
                    folderField.setText(selectFile.getAbsolutePath());
                    updateTabTitle();
                }
            }
            else{
                if(type == FolderTable.Type.LEFT){
                    selectFile = node.getPathLeft();
                }
                else{
                    selectFile = node.getPathRight();
                }
            }
            treeTable.setFile(selectFile, ()->{
                updateTable();
                otherFolderCompare.updateTable();
            });

        }

        private void updateTabTitle(){
            String path1 = this.getFolderField().getText();
            String path2 = otherFolderCompare.getFolderField().getText();
            if(!"".equals(path1) && !"".equals(path2)){
                File fileLeft = new File(path1);
                File fileRight = new File(path2);
                String name = null;
                if(type == FolderTable.Type.LEFT){
                    name = fileLeft.getName() + "<-->" + fileRight.getName();
                }
                else if(type == FolderTable.Type.RIGHT){
                    name = fileRight.getName() + "<-->" + fileLeft.getName();
                }
                DesktopFrameFolderDiff.this.setTitle(name);
                getProjectNode().setTitle(name + "-文件夹对比");
                DFMain.projectTable.refreshUI();
            }
        }

        private void initPanel(FolderTable.Type type){
            add(initAddressPanel(), new GB(0,0,1,1).setFill(GB.BOTH).setWeight(100,0));
            treeTable = new FolderTable(root, type);
            add(new JScrollPane(treeTable),new GB(0,1,1,1).setFill(GB.BOTH).setWeight(100,100));
        }

        public void updateTable(){
            treeTable.refreshUI();
        }

        public JTextField getFolderField() {
            return folderField;
        }

        public void setOtherFolderCompare(FolderComparePanel otherFolderCompare) {
            this.otherFolderCompare = otherFolderCompare;
            getTreeTable().addExpansionListener(new FolderTable.SelfExpansionListener() {
                @Override
                public void treeExpanded(TreeExpansionEvent event) {
                    FolderTable otherTreeTable = otherFolderCompare.getTreeTable();
                    otherTreeTable.expandPath(event.getPath());
                }

                @Override
                public void treeCollapsed(TreeExpansionEvent event) {
                    FolderTable otherTreeTable = otherFolderCompare.getTreeTable();
                    otherTreeTable.collapsePath(event.getPath());
                }
            });

            getTreeTable().addFileDiffListener((path) -> {
                String path1 = this.getFolderField().getText();
                String path2 = otherFolderCompare.getFolderField().getText();
                File fileLeft = new File(path1, path);
                File fileRight = new File(path2, path);
                ProjectNode node = null;

                if(type == FolderTable.Type.LEFT){
                    String name = fileLeft.getName() + "<->" + fileRight.getName();
                    node = new ProjectNode(name, fileLeft, fileRight, ProjectNode.ProjectType.FILE_DIFF);

                }
                else if(type == FolderTable.Type.RIGHT){
                    String name = fileRight.getName() + "<->" + fileLeft.getName();
                    node = new ProjectNode(name, fileRight, fileLeft, ProjectNode.ProjectType.FILE_DIFF);
                }

                DFMain.projectTable.addNode(node);
                DesktopFrame frame = new DesktopFrameFileDiff(node);
                DFMain.desktopPane.add(frame);
                frame.setVisible(true);
            });
        }

        public FolderTable getTreeTable() {
            return treeTable;
        }
    }
}
